package system

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/system"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var SysOauth = cSystemSys_oauth{}

type cSystemSys_oauth struct{}

// GetSysOauthList 获取列表
func (u *cSystemSys_oauth) GetSysOauthList(ctx context.Context, req *system.GetSysOauthListReq) (res *system.GetSysOauthListRes, err error) {
	var reqData = new(model.GetSysOauthListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.SystemSysOauth().GetSysOauthList(ctx, reqData)
	res = new(system.GetSysOauthListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetSysOauthById 获取指定ID数据
func (u *cSystemSys_oauth) GetSysOauthById(ctx context.Context, req *system.GetSysOauthByIdReq) (res *system.GetSysOauthByIdRes, err error) {
	data, err := service.SystemSysOauth().GetSysOauthById(ctx, req.Id)
	res = new(system.GetSysOauthByIdRes)
	gconv.Scan(data, &res)
	return
}

// AddSysOauth 添加数据
func (u *cSystemSys_oauth) AddSysOauth(ctx context.Context, req *system.AddSysOauthReq) (res *system.AddSysOauthRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SysOauthAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.SystemSysOauth().AddSysOauth(ctx, data)
	return
}

// EditSysOauth 修改数据
func (u *cSystemSys_oauth) EditSysOauth(ctx context.Context, req *system.EditSysOauthReq) (res *system.EditSysOauthRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.SysOauthEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.SystemSysOauth().EditSysOauth(ctx, data)
	return
}

// DeleteSysOauth 删除数据
func (u *cSystemSys_oauth) DeleteSysOauth(ctx context.Context, req *system.DeleteSysOauthReq) (res *system.DeleteSysOauthRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.SystemSysOauth().DeleteSysOauth(ctx, req.Ids)
	return
}
